package mwa.cs.dao;

import java.util.List;
import java.util.Map;

import mwa.cs.database.Database;
import mwa.cs.model.DefaultAddress;

/**
 * A class used for managing default address
 */
public class DefaultAddressDAO {

	private Database db;

	public DefaultAddressDAO(Database db) {
		this.db = db;
	}

	public DefaultAddress getDefaultAddress(int pnSiteId) {

		String vaSql = "SELECT * FROM DEFAULT_ADDRESS WHERE SITE_ID = ?";
		List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);

		if (result != null && result.size() > 0) {
			int vnSiteId = Integer.parseInt(result.get(0).get("SITE_ID")
					.toString());

			String vaCityCode = result.get(0).get("CITY_CODE").toString();
			String vaDistrictCode = result.get(0).get("DISTRICT_CODE")
					.toString();
			String vaProvinceCode = result.get(0).get("PROVINCE_CODE")
					.toString();
			String vaPostCode = result.get(0).get("POST_CODE").toString();

			int vnInsertUserId = Integer.parseInt(result.get(0)
					.get("INSERT_USER_ID").toString());
			String vaInsertDate = result.get(0).get("INSERT_DATE").toString();
			String vaInsertTime = result.get(0).get("INSERT_TIME").toString();
			int vnUpdateUserId = Integer.parseInt(result.get(0)
					.get("UPDATE_USER_ID").toString());
			String vaUpdateDate = result.get(0).get("UPDATE_DATE").toString();
			String vaUpdateTime = result.get(0).get("UPDATE_TIME").toString();

			DefaultAddress voDa = new DefaultAddress(vnSiteId, vaCityCode,
					vaDistrictCode, vaProvinceCode, vaPostCode, vnInsertUserId,
					vaInsertDate, vaInsertTime, vnUpdateUserId, vaUpdateDate,
					vaUpdateTime);
			return voDa;
		} else {
			return null;
		}
	}
	
	public DefaultAddress getDefaultAddressBySiteId(int pnSiteId) {
		String vaSql = "SELECT site_id,city_code,district_code,province_code,post_code FROM default_address WHERE site_id = ?";
		 Map<String, Object> result = db.querySingle(vaSql, pnSiteId);
		DefaultAddress voDefaultAddress = new DefaultAddress();
		if (result != null ) {
			voDefaultAddress.setVnSiteId( Integer.parseInt(result.get("SITE_ID").toString()));
			voDefaultAddress.setVaCityCode( result.get("CITY_CODE").toString());
			voDefaultAddress.setVaDistrictCode( result.get("DISTRICT_CODE").toString());
			voDefaultAddress.setVaProvinceCode( result.get("PROVINCE_CODE").toString());
			voDefaultAddress.setVaPostCode( result.get("POST_CODE").toString());
		} else {
			result = null;
		}
		return voDefaultAddress;
	}

	public boolean hasDefaultAddress(int pnSiteId) {
		String vaSql = "SELECT * FROM DEFAULT_ADDRESS WHERE SITE_ID = ?";
		List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);

		if (result != null && result.size() > 0) {
			return true;
		} else {
			return false;
		}
	}

	public boolean insert(DefaultAddress poDa) {
		String vaSql = " insert into DEFAULT_ADDRESS (SITE_ID,CITY_CODE,DISTRICT_CODE,PROVINCE_CODE,POST_CODE,INSERT_USER_ID,INSERT_DATE,INSERT_TIME,UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME) "
				+ "values (?,?,?,?,?,?,?,?,?,?,?) ";

		try {
			db.insert(vaSql, poDa.getVnSiteId(), poDa.getVaCityCode(), poDa.getVaDistrictCode(), 
					poDa.getVaProvinceCode(), poDa.getVaPostCode(),
					poDa.getVnInsertUserId(), poDa.getVaInsertDate(), poDa.getVaInsertTime(), 
					poDa.getVnUpdateUserId(), poDa.getVaUpdateDate(),poDa.getVaUpdateTime());
			
		} catch (Exception ex) {
			return false;
		}
		return true;
	}
	
	public boolean update(DefaultAddress poDa) {
		String vaSql = " update DEFAULT_ADDRESS set CITY_CODE = ?, DISTRICT_CODE = ?, PROVINCE_CODE = ?, POST_CODE = ?," 
				+ " UPDATE_USER_ID = ?, UPDATE_DATE = ?, UPDATE_TIME = ? "
				+ " where SITE_ID = ? ";
		try {
			db.update(vaSql, poDa.getVaCityCode(), poDa.getVaDistrictCode(), poDa.getVaProvinceCode(), poDa.getVaPostCode(),
					poDa.getVnUpdateUserId(), poDa.getVaUpdateDate(), poDa.getVaUpdateTime(), poDa.getVnSiteId());
		} catch (Exception ex) {
			return false;
		}
		return true;
	}
	
	public boolean delete(int pnSiteId) {
		String vaSql = " delete from DEFAULT_ADDRESS where SITE_ID = ? ";
		try {
			db.update(vaSql, pnSiteId);
		} catch (Exception ex) {
			return false;
		}
		return true;
	}

}
